#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using NUnit.Framework;
using Utilities.Collections;
using Utilities.Miscellaneous;
using Utilities.Testing;
using System.Collections.Generic;

namespace Utilities.Test.Collections
{
  public class EnumerableUtilsFixture : TestFixtureBase
  {
    [Test]
    public void ToStringSimple()
    {
      string[] texts = new string[] {"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"};
      object[] miscValues = new object[] {new Version(1, 0, 9, 8), "I am a string", 99, 1.567, new object(), null};
      string result;

      result = EnumerableUtils.ToString(texts);
      Assert.AreEqual("The, quick, brown, fox, jumped, over, the, lazy, dog", result);

      result = EnumerableUtils.ToString(miscValues);
      Assert.AreEqual("1.0.9.8, I am a string, 99, 1.567, System.Object, {null}", result);
    }

    [Test]
    public void ToStringDelegate()
    {
      string[] texts = new string[] { "The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" };
      object[] miscValues = new object[] { new Version(1, 0, 9, 8), "I am a string", 99, 1.567, new object(), null };
      string result;

      result = EnumerableUtils.ToString(texts, delegate (object value) { return @"'" + MiscellaneousUtils.ToString(value) + @"'"; }, "|");
      Assert.AreEqual(@"'The'|'quick'|'brown'|'fox'|'jumped'|'over'|'the'|'lazy'|'dog'", result);

      result = EnumerableUtils.ToString(miscValues, delegate(object value) { return @"'" + MiscellaneousUtils.ToString(value).ToLowerInvariant() + @"'"; }, string.Empty);
      Assert.AreEqual(@"'1.0.9.8''i am a string''99''1.567''system.object''{null}'", result);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: enumerable")]
    public void ToStringNullEnumerable()
    {
      EnumerableUtils.ToString(null);
    }

    [Test]
    public void Convert()
    {
      List<string> stringList = CollectionUtils.CreateList("1", "2", "3");
      IEnumerable<int> intEnumerable = EnumerableUtils.Convert<int>(stringList);
      Assert.IsNotNull(intEnumerable);

      List<int> intList = new List<int>(intEnumerable);
      Assert.AreEqual(3, intList.Count);
      Assert.AreEqual(1, intList[0]);
      Assert.AreEqual(2, intList[1]);
      Assert.AreEqual(3, intList[2]);
    }
  }
}